#include <iostream>
#include <cstdlib>
using namespace std;

class Student{
    private:
        string name;
        int *age;
    public:
        Student();
        Student(string n, int a);
        // 类内定义的写法
        #if 0
        ~Student(){
            cout << "析构函数" << endl;
            delete age;
            age = nullptr;
        }
        #else
        // 类内声明的写法
        ~Student();
        #endif
        void show();
};

Student::Student(){
    cout << "无参构造函数" << endl;
}
Student::Student(string n, int a):name(n), age(new int(a)){
    cout << "有参构造函数" << endl;
}
void Student::show(){
    cout << name << " " << *age << endl;
}
Student::~Student(){
    cout << "析构函数" << endl;
    delete age;
    age = nullptr;
}

int main(int argc, const char *argv[]){
    // 栈区
    Student s1("zhangsan", 18); // 调用有参构造
    s1.show(); // zhangsan  18

    // 堆区
    Student *p1 = new Student("lisi", 20);
    p1->show();
    delete p1; // 会调用析构函数
    p1 = nullptr;

    // malloc/free 是不会自动调用 构造和析构 函数的  下面的例子就不会调用
    Student *p2 = (Student *)malloc(sizeof(Student));
    free(p2);
    p2 = nullptr;

    return 0;
}